package com.sihan.framework.etm.controller;

import com.framework.model.ApiResult;
import com.sihan.framework.etm.dto.DevicePushDTO;
import com.sihan.framework.etm.dto.PageDevicePush;
import com.sihan.framework.etm.entity.DevicePush;
import com.sihan.framework.etm.service.DevicePushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "设备数据推送配置")
@RestController
@RequestMapping("/device/push")
public class DevicePushController {

    @Autowired
    private DevicePushService devicePushService;

    @ApiOperation(value = "分页获取数据", notes = "分页获取数据")
    @PostMapping("/pageList")
    public ApiResult<?> addDevicePush(@RequestBody PageDevicePush pageDevicePush){
        try{
            return ApiResult.success(devicePushService.pageListDevicePushVO(pageDevicePush));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ApiResult.fail("获取失败");
    }


    @ApiOperation(value = "添加推送方式", notes = "添加推送方式")
    @PostMapping("/add")
    public ApiResult<?> addDevicePush(@RequestBody List<DevicePushDTO> li){

        List<DevicePush> devicePushes = new ArrayList<>();
        for (DevicePushDTO dto:li){
            DevicePush devicePush = new DevicePush();
            BeanUtils.copyProperties(dto, devicePush);
            devicePushes.add(devicePush);
        }
        boolean b = devicePushService.batchInsert(devicePushes);
        if (b){
            return ApiResult.success("添加成功");
        }
        return ApiResult.fail("添加失败");
    }


    @ApiOperation(value = "更新推送状态", notes = "更新推送状态")
    @PostMapping("/update")
    public ApiResult<?> updateDevicePush(@RequestBody DevicePush devicePush){

        boolean b = devicePushService.saveOrUpdate(devicePush);
        if (b){
            return ApiResult.success("更新成功");
        }
        return ApiResult.fail("更新失败");
    }


    @ApiOperation(value = "删除数据", notes = "删除数据")
    @PostMapping("/del")
    public ApiResult<?> del(@RequestBody String id){

        boolean b = devicePushService.deleteById(id);
        if (b){
            return ApiResult.success("删除成功");
        }
        return ApiResult.fail("删除失败");
    }




}
